import uuid
import os
import datetime


def manif(path="./B/"):
    manifest = """
    {
        "format_version": 1,
        "header": {
            "description": "A Video Pictures Particle Resource Player",
            "name": "Minecraft Video Player Generate Player",
            "uuid": "2281e703-a42d-4683-96eb-e18daa08cdd0",
            "version": [0, 0, 1],
            "min_engine_version": [0, 0, 1]
        },
        "modules": [
            {
                "description": "bgArray",
                "type": "data",
                "uuid": "2a407c8d-8b0a-44f0-b773-25cc1e81d5ec",
                "version": [0, 0, 1]
            }
        ]
    }
    """
    new_manifest = manifest.replace("2281e703-a42d-4683-96eb-e18daa08cdd0", str(uuid.uuid4()))\
        .replace("2a407c8d-8b0a-44f0-b773-25cc1e81d5ec", str(uuid.uuid4()))\
        .replace("A Video Pictures Particle Resource Player",
                 "A Video Pictures Particle Resource Player; date:" + str(datetime.datetime.now()))
    try:
        with open(path + "manifest.json", "w") as f:
            f.write(new_manifest)
    except FileNotFoundError as e:
        # print(e)
        if "./B/manifest.json" in str(e):
            # print("T")
            os.mkdir(path)
            with open(path + "manifest.json", "w") as f:
                f.write(new_manifest)
        else:
            return False


def funcs(is1000cutDown=False, Debug=False,
          high_version=False):
    try:
        os.mkdir("./B/functions")
    except FileExistsError:
        pass

    if Debug:
        tick = 0
        bigV = 0
        for k in os.listdir("./resources/y/"):
            if ".png" in k:
                bigV += 1
        for i in range(int(list(os.listdir("./resources/y/")).__len__() / 1000) + 1):
            with open("./B/functions/play" + str(i) + ".mcfunction", 'w') as f:
                f.write("scoreboard players add @a tick 1\n")
                last = 0
                for j in range(1000):
                    if j + tick + 1 > bigV:
                        break
                    f.write("execute @a[scores={tick=" + str(j + tick) + "}] ~~~ particle m" + str(j + tick + 1) +
                            " ~ -45 ~\n")
                    last = j + tick
                f.write("execute @a[scores={tick=" + str(last) + "..}] ~~~ scoreboard players set @a tick " +
                        str(j + tick) + "\n")
                f.write("execute @a[scores={tick=" + str(last) + "..}] ~~~ setblock ~ ~1 ~ air")
            tick += 1000
    else:
        try:
            if is1000cutDown:
                tick = 0
                for i in range(int(list(os.listdir("./R/textures/particle/mvp/")).__len__() / 2000) + 1):
                    with open("./B/functions/play" + str(i) + ".mcfunction", 'w') as f:
                        f.write("scoreboard players add @a tick 1\n")
                        last = 0
                        for j in range(2000):
                            f.write("execute @a[scores={tick=" + str(j + i) + "}] ~~~ particle m" + str(j + i + 1) +
                                    " ~ -45 ~\n")
                            last = j + i
                        f.write("execute @a[scores={tick=" + str(last) + "..}] ~~~ scoreboard players set @a tick 0\n")
                    tick += 2000
            else:
                with open("./B/functions/play.mcfunction", 'w') as f:
                    last = 0
                    for i in range(list(os.listdir("./R/textures/particle/mvp/")).__len__()):
                        if high_version:
                            f.write(
                                "execute as @a[scores={tick=" + str(i) + "}] run"
                                                                         " particle m" + str(i + 1) + " ~ -45 ~\n")
                        else:
                            f.write(
                                "execute @a[scores={tick=" + str(i) + "}] ~~~ particle m" + str(i + 1) + " ~ -45 ~\n")
                        last = i
                    if high_version:
                        f.write("execute as @a[scores={tick=" + str(last) + "..}] run"
                                                                            " scoreboard players set @a tick 0\n")
                    else:
                        f.write("execute @a[scores={tick=" + str(last) + "..}] ~~~ scoreboard players set @a tick 0\n")
                    f.write("scoreboard players add @a tick 1\n")
        except FileNotFoundError:
            if is1000cutDown:
                tick = 0
                for i in range(int(list(os.listdir("./resources/")).__len__() / 2000) + 1):
                    with open("./B/functions/play" + str(i) + ".mcfunction", 'w') as f:
                        last = 0
                        for j in range(2000):
                            f.write("execute @a[scores={tick=" + str(j + i) + "}] ~~~ particle m" + str(j + i + 1) +
                                    " ~ -45 ~\n")
                            last = j + i
                        f.write("execute @a[scores={tick=" + str(last) + "..}] ~~~ scoreboard players set @a tick 0\n")
                    tick += 2000
            else:
                with open("./B/functions/play.mcfunction", 'w') as f:
                    f.write("scoreboard players add @a tick 1\n")
                    last = 0
                    for i in range(list(os.listdir("./resources/")).__len__()):
                        if high_version:
                            f.write(
                                "execute as @a[scores={tick=" + str(i) + "}] run"
                                                                         " particle m" + str(i + 1) + " ~ -45 ~\n")
                        else:
                            f.write(
                                "execute @a[scores={tick=" + str(i) + "}] ~~~ particle m" + str(i + 1) + " ~ -45 ~\n")
                        last = i
                    if high_version:
                        f.write("execute as @a[scores={tick=" + str(last) + "..}] run"
                                                                            " scoreboard players set @a tick 0\n")
                    else:
                        f.write("execute @a[scores={tick=" + str(last) + "..}] ~~~ scoreboard players set @a tick 0\n")
                    f.write("scoreboard players add @a tick 1\n")


if __name__ == '__main__':
    manif()
    # funcs(Debug=True)
    # funcs(high_version=True)
    funcs()
